'''
Created on Feb 14, 2011

@author: brendenjeon

from :
http://club.filltong.net/codingdojo/17705
http://club.filltong.net/codingdojo/27916

http://club.filltong.net/codingdojo/26202

'''
import unittest

'''
http://club.filltong.net/codingdojo/17705
'''
def reversed_number(former, latter):
    result = int(former[::-1]) + int(latter[::-1])
    return str(result)[::-1]

'''
http://club.filltong.net/codingdojo/27916
'''
class Node(object):
    def __init__(self, name):
        self.name = name
        self.link = []

    def add_link(self, node):
        self.link.append(node)

    def is_self(self, node):
        if self.name == node.name:
            return True
        return False

    def is_circle(self, check_node):
        for node in self.link:
            if node.is_self(check_node):
                return True
            else:
                node.is_circle(check_node)
        return False

    def get_linked_node(self):
        if not self.link:
            return False
        l = self.link[:]
        for n in self.link:
            if n.get_linked_node():
                for cn in n.get_linked_node():
                    if not cn in l:
                        l.append(cn)
        return l

    def __str__(self):
        return '%s' % self.name

class Dependencies(object):
    def __init__(self):
        self.nodes = []

    def get_node(self, name):
        cap_name = name.capitalize()
        for n in self.nodes:
            if n.name == cap_name:
                return n
        new_node = Node(cap_name)
        self.nodes.append(new_node)
        return new_node

    def add_direct(self, node_name, dependable_node_names):
        n = self.get_node(node_name)
        for dnn in dependable_node_names:
            n.add_link(self.get_node(dnn))

    def dependencies_for(self, node_name):
        n = self.get_node(node_name)
        return sorted([cn.name for cn in n.get_linked_node()])
'''
'''
def change(s, x, char):
    l = list(s)
    l[x] = char
    return ''.join(l)

def reverse(s):
    if s == 'U':
        return 'D'
    elif s == 'D':
        return 'U'
    else:
        return s
def remove_x(input, y):
    for x in range(len(input[0])):
        i = 0
        while y-i != 0:
            if input[y-i][x] == 'x':
                j = 1
                while input[y-j][x] == 'x':
                    j = j + 1
                    if j > y:
                        break
                if j == len(input) or j > y:
                    i = i + 1
                    continue
                input[y] = change(input[y], x, reverse(input[y-j][x]))
                input[y-j] = change(input[y-j], x, 'x')
            i = i + 1

def process(input, y, x):
    d = ['D', 'L', 'U', 'R']

    if y > 0:
        if input[y-1][x] == d[0]:
            input[y-1] = change(input[y-1], x, 'x')
            process(input, y-1, x)
    if y < len(input)-1:
        if input[y+1][x] == d[2]:
            input[y+1] = change(input[y+1], x, 'x')
            process(input, y+1, x)
    if x > 0:
        if input[y][x-1] == d[3]:
            input[y] = change(input[y], x-1, 'x')
            process(input, y, x-1)
    if x < len(input[0])-1:
        if input[y][x+1] == d[1]:
            input[y] = change(input[y], x+1, 'x')
            process(input, y, x+1)

def bomb(input):
    for i, row in enumerate(input):
        if row[0] == 'L':
            input[i] = change(row, 0, 'x')
            process(input, i, 0)
            remove_x(input, i)
    return input


class Test(unittest.TestCase):
    def _test_reversed_number(self):
        self.assertEqual('34', reversed_number('24', '1'))
        self.assertEqual('1998', reversed_number('4358', '754'))

    def _test_dependency(self):
        dep = Dependencies()
        dep.add_direct('A', ['B', 'C'] )
        dep.add_direct('B', ['C', 'E'] )
        dep.add_direct('C', ['G'])
        dep.add_direct('D', ['A', 'F'])
        dep.add_direct('E', ['F'])
        dep.add_direct('F', ['H'])
        dep.add_direct('H', ['A'])
#        a = dep.get_node('A')
#        print a.is_circle(a)
#        self.assertEqual(['B', 'C', 'E', 'F', 'G', 'H'], dep.dependencies_for('A'))
#        self.assertEqual(['F', 'H'], dep.dependencies_for('E'))

    def _test_bomb(self):
        self.assertEqual(['xxxLL','DxxxR'], bomb(['LLxLL','DULLR']))
        self.assertEqual(['xxxxx','xxxxx','xxxxx','xxUxx'],
            bomb(['Dxxxx','DLxxx','LLxxx','ULUxx']))
        self.assertEqual(['xxxxx','DDDDD'], bomb(['UUUUU','LLLLL']))
        self.assertEqual(['xxxxx','xxxxx','DDDDD'], bomb(['UUUUU','RRRRD','LLLLL']))
        self.assertEqual(['xxxxx','xxxxx','xxxxx'], bomb(['RRDLL','LLRRD','LLLLL']))


if __name__ == "__main__":
    unittest.main()